<?php

namespace Ibw\JobeetBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Ibw\JobeetBundle\Entity\Affiliate;
use Ibw\JobeetBundle\Form\AffiliateType;
use Ibw\JobeetBundle\Entity\Category;

/*
 * Affiliate controller.
 *
 * @Route("/affiliate")
 */

class AffiliateController extends Controller {

    /**
     *  Affiliate entities.
     *
     * @Route("/{_locale}/new", name="ibw_affiliate_new")
     */
    public function newAction() {
        $entity = new Affiliate();
        $form = $this->createForm(new AffiliateType(), $entity);

        return $this->render('IbwJobeetBundle:Affiliate:affiliate_new.html.twig', array(
                    'entity' => $entity,
                    'form' => $form->createView(),
        ));
    }

     /**
     * Create Affiliate Entities 
     *
     * @Route("/{_locale}/create", name="ibw_affiliate_create")
     * @Method("POST")
     
     */
    public function createAction(Request $request) {
        $affiliate = new Affiliate();
        $form = $this->createForm(new AffiliateType(), $affiliate);
        $form->bind($request);
        $em = $this->getDoctrine()->getManager();

        if ($form->isValid()) {

            $formData = $request->get('affiliate');
            $affiliate->setUrl($formData['url']);
            $affiliate->setEmail($formData['email']);
            $affiliate->setIsActive(false);

            $em->persist($affiliate);
            $em->flush();

            return $this->redirect($this->generateUrl('ibw_affiliate_wait'));
        }

        return $this->render('IbwJobeetBundle:Affiliate:affiliate_new.html.twig', array(
                    'entity' => $affiliate,
                    'form' => $form->createView(),
        ));
    }
    
     /**
     * wait
     *
     * @Route("/{_locale}/wait", name="ibw_affiliate_wait")
     
     */
    public function waitAction()
    {
        return $this->render('IbwJobeetBundle:Affiliate:wait.html.twig');
    }

}
